//
//  TaskItem.swift
//  SwiftUILean
//
//  Created by wyknmjj on 2024/5/14.
//

import SwiftUI
import SwiftData

@Model
class TaskItem:Identifiable{
    var id:UUID
    var taskTitle: String
    var creationDate: Date
    var isCompleted:Bool
    var tInt: String
    
    init(id: UUID = .init(), taskTitle: String, creationDate: Date = .init(), isCompleted: Bool = false, tInt: String) {
        self.id = id
        self.taskTitle = taskTitle
        self.creationDate = creationDate
        self.isCompleted = isCompleted
        self.tInt = tInt
    }
    
    var tintColor:Color{
        return getTintColor(tInt)
    }
}


func getTintColor(_ tInt:String)->Color{
    switch tInt{
    case "TaskColor1":return .taskColor1
    case "TaskColor2":return .taskColor2
    case "TaskColor3":return .taskColor3
    case "TaskColor4":return .taskColor4
    default:return .black
    }
}


extension Date{
    static func updateHour(_ n:Int)->Date{
        let c = Calendar.current
        return c.date(byAdding: .hour, value: n, to: .init()) ?? .init()
    }
    
    func format(_ format:String) -> String{
        let f = DateFormatter()
        f.dateFormat = format
        
        return f.string(from: self)
    }
    
    
    
    func fetchWeek(_ d:Date = .init()) -> [WeekDay]{
        let c = Calendar.current
        let startOfDate = c.startOfDay(for: d)
        
        var week: [WeekDay] = []
        
        let weekForDate = c.dateInterval(of: .weekOfMonth, for: startOfDate)
        
        guard let startOfWeek = weekForDate?.start else{
            return []
        }
        
        (0..<7).forEach { index in
            if let weekDay = c.date(byAdding: .day, value: index, to: startOfWeek){
                week.append(.init(date: weekDay))
            }
        }
        return week
    }
    struct WeekDay: Identifiable{
        var id: UUID = .init()
        var date: Date
    }
    
    var isToday:Bool{
        return Calendar.current.isDateInToday(self)
    }
    
    
    
    func createNextWeek()-> [WeekDay]{
        let c = Calendar.current
        let startOfLastDate = c.startOfDay(for: self)
        guard let nextDate = c.date(byAdding: .day,value: 1, to: startOfLastDate)else{
            return []
        }
        return fetchWeek(nextDate)
    }
    func createPreviousWeek()-> [WeekDay]{
        let c = Calendar.current
        let startOfLastDate = c.startOfDay(for: self)
        guard let previousDate = c.date(byAdding: .day,value: -1, to: startOfLastDate)else{
            return []
        }
        return fetchWeek(previousDate)
    }
    
    var isSameHour:Bool{
        return Calendar.current.compare(self, to: .init(), toGranularity: .hour) == .orderedSame
    }
    
    var isPast:Bool{
        return Calendar.current.compare(self, to: .init(), toGranularity: .hour) == .orderedAscending
    }
}

extension Color{
    static let BG:Color = .blue
    static let taskColor1:Color = .yellow
    static let taskColor2:Color = .red
    static let taskColor3:Color = .blue
    static let taskColor4:Color = .green
}
